package com.itic.system.user.withdraw.service;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.type.*;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.system.user.user.persistence.model.HppUser;
import com.itic.system.user.user.service.HppUserService;
import com.itic.system.vo.WithDrawListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.user.withdraw.persistence.dao.UserWithDrawRecordDao;
import com.itic.system.user.withdraw.persistence.model.UserWithDrawRecord;
import com.itic.system.user.withdraw.persistence.model.UserWithDrawRecordExample;
import com.itic.system.user.withdraw.persistence.model.UserWithDrawRecordExample.Criteria;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class UserWithDrawRecordService
     extends CrudService<UserWithDrawRecordDao,UserWithDrawRecord,UserWithDrawRecordExample> {
    @Autowired
    private UserWithDrawRecordDao userWithDrawRecordDao;
    @Autowired
    private HppUserService hppUserService;

    /**
     * 申请提现
     * @param userId
     * @param money
     * @return
     */
    public Map<String,Object> applyWithDrawRecord(String userId,double money){
        Map<String,Object> resultMap = new HashMap<>();
        //效验用户余额是否足够
        HppUser hppUser = this.hppUserService.get(userId);
        if(hppUser!=null){
            double userBalance = hppUser.getMoney()==null?0:hppUser.getMoney();
            //效验用户余额是否足够
            if(userBalance >=money){
                //操作用户余额  并记录余额日志 以及 提现记录
               String result = this.hppUserService.operateUserMoney(userId,money, OperateType.sub, FlowType.ExPend, FlowMethod.WithDraw,null,null,null);
               if(SUCCESS.equals(result)){
                   resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                   resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
               }else{
                   // 提示申请失败
                   resultMap.put(BaseCode.CODE, BaseCode.HPP_WITHDRAW_APPLY_FAIL_CODE);
                   resultMap.put(BaseCode.MSG, BaseCode.HPP_WITHDRAW_APPLY_FAIL_MSG);
               }
            }else{
                //提示用户余额不足
                resultMap.put(BaseCode.CODE, BaseCode.HPP_USER_MONEY_NOT_ENOUGE_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.HPP_USER_MONEY_NOT_ENOUGE_MSG);
            }
        }else{
            //提示用户不存在
            resultMap.put(BaseCode.CODE, BaseCode.HPP_USER_NOT_EXSIT_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.HPP_USER_NOT_EXSIT_MSG);
        }
        return  resultMap;
    }

    /**
     * 计算提现服务费(含手续费)
     * @param money 提现金额
     * @return
     */
    public double calServiceCharge(double money){
        String serviceCharge = "0";
        String percent = NumberStringHelper.divString(ServiceCharge.Percent.getPercent(),"100",2);
        serviceCharge = NumberStringHelper.mulString(Double.toString(money),percent,2);
        return Double.parseDouble(serviceCharge);
    }

    /**
     * 申请提现记录
     * @param user
     * @param money
     * @param balanceLogId
     * @return
     */
    public String applyWithDrawRecord(HppUser user,double money,String balanceLogId){
        double serviceCharge = this.calServiceCharge(money);
        double amount = money - serviceCharge;
        return this.addWithDrawRecord(amount,serviceCharge,user.getId(),balanceLogId);
    }

    /**
     *   添加提现记录
     * @param amount
     * @param serviceCharge
     * @param userId
     * @param balanceLogId
     * @return
     */
    public String addWithDrawRecord(double amount,double serviceCharge,String userId,String balanceLogId){
        UserWithDrawRecord userWithDrawRecord = new UserWithDrawRecord();
        userWithDrawRecord.setMoney(amount);
        userWithDrawRecord.setServiceCharge(serviceCharge);
        userWithDrawRecord.setUserId(userId);
        userWithDrawRecord.setUserBalanceLogId(balanceLogId);
        userWithDrawRecord.setStatus(WithDrawStatus.Apply.getStatus());
        return this.add(userWithDrawRecord);
    }

    /**
     * 分页查询某用户的提现记录
     * @param userId 用户Id
     * @param example 分页
     * @return
     */
    public List<UserWithDrawRecord> findUserWithDrawRecordByPageAndUserId(String userId,UserWithDrawRecordExample example){
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(userId);
        example.setOrderByClause(" create_time desc ");
        return this.list(example);
    }

    /**
     * 将 UserWithDrawRecord 转换成 WithDrawListVo
     * @param userWithDrawRecord
     * @return
     */
    public WithDrawListVo UserWithDrawRecordWrapToWithDrawListVo(UserWithDrawRecord userWithDrawRecord){
        WithDrawListVo withDrawListVo = new WithDrawListVo();
        withDrawListVo.setCreateTime(userWithDrawRecord.getCreateTime());
        WithDrawStatus withDrawStatus = WithDrawStatus.getWithDrawStatus(userWithDrawRecord.getStatus());
        withDrawListVo.setStatus(withDrawStatus.getStatus());
        withDrawListVo.setStatusContent(withDrawStatus.getName());
        withDrawListVo.setMoney(userWithDrawRecord.getMoney());
        return withDrawListVo;
    }

}